import java.io.*;
import java.util.ArrayList;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class MRTIndexer {
		
	FSDirectory dir;
	StandardAnalyzer analyzer;
	IndexWriterConfig config;
	IndexWriter writer;
	IndexReader reader;
	IndexSearcher searcher;
	
	ScoreDoc[] hits;
	
	String directory = "C:\\Users\\vthai\\Documents\\";
	
	File logdir = new File(directory + "MRTIndexerLogs");
	File index = new File(directory + "MRTIndex");
	
	int searchSize = 20;
	
	/**
	 * MRTIndexer
	 * Description: Constructor
	 * @throws IOException 
	 */
	MRTIndexer() throws IOException {
		dir = FSDirectory.open(index);
		
		analyzer = new StandardAnalyzer(Version.LUCENE_35);
		
		config = new IndexWriterConfig(Version.LUCENE_35, analyzer);
		
		config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
		
		writer = new IndexWriter(dir,config);		
	}
	
	/**
	 * compileLogs
	 * Description: prep mrt logs for indexing
	 * @return
	 */
	private ArrayList<File> compileLogs() {
		ArrayList<File> dirFiles = new ArrayList<File>();
		
		for(File f : logdir.listFiles()) {
			String filename = f.getName().toLowerCase();
						
			if(filename.endsWith(".log")) {
				dirFiles.add(f);
			}
		}
		return dirFiles;
	}
	
	/**
	 * indexLogs
	 * Description: index logs
	 * @throws IOException 
	 * @throws CorruptIndexException 
	 */
	public void indexLogs() throws CorruptIndexException, IOException {
		ArrayList<File> logs = compileLogs();
		
		for(File f : logs) {
			FileReader fr = null;
			try {
				Document doc = new Document();
			
				fr = new FileReader(f);
				
				doc.add(new Field("title", f.getName(),Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field("contents",fr));
				doc.add(new Field("path",directory,Field.Store.YES,Field.Index.NOT_ANALYZED));
				
				writer.addDocument(doc);
				
				fr.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		writer.close();
	}
	
	/**
	 * queryLogs
	 * Description: Constructs a query object and executes it against the current index
	 * @param s
	 * @throws ParseException 
	 * @throws IOException 
	 * @throws CorruptIndexException 
	 */
	public void queryLogs(String s) throws ParseException, CorruptIndexException, IOException {
		Query q = new QueryParser(Version.LUCENE_35, "title", analyzer).parse(s);
		
		searcher = new IndexSearcher(IndexReader.open(dir, true));

		hits = searcher.search(q, searchSize).scoreDocs;
		
	}
	
	/**
	 * printLogs
	 * Description: Prints out the logs that were queried
	 * @throws IOException 
	 * @throws CorruptIndexException 
	 */
	public void printLogs() throws CorruptIndexException, IOException {
		
		System.out.println("\nFound " + hits.length + " log(s)");
		System.out.println("--------------");
		
		for(ScoreDoc d : hits) {
			System.out.print("\n" + searcher.doc(d.doc).get("title") + "\n");
		}
		
	}
	
	
	public static void main(String args[]) throws IOException, ParseException {
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		MRTIndexer indexer = new MRTIndexer();
		indexer.indexLogs();
		
		System.out.print("Please enter a query or q to quit: ");
		
		String input = br.readLine();
		while(!input.equalsIgnoreCase("q")) {
			indexer.queryLogs(input);
			indexer.printLogs();
			
			System.out.print("\nPlease enter a query or q to quit: ");
			
			input = br.readLine();
		}
		
		System.out.println("\nExiting MRTIndexer...");
		
		
	}
}
